package com.jkoss.biz.imp;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jkoss.biz.IStusBiz;
import com.jkoss.dao.kc.KaoshiMapper;
import com.jkoss.dao.stu.ClassesMapper;
import com.jkoss.dao.stu.EvaluateMapper;
import com.jkoss.dao.stu.OssstuMapper;
import com.jkoss.dao.stu.ResultsMapper;
import com.jkoss.dao.zx.ConsultationMapper;
import com.jkoss.pojo.kc.Kaoshi;
import com.jkoss.pojo.stu.Classes;
import com.jkoss.pojo.stu.Evaluate;
import com.jkoss.pojo.stu.EvaluateExample;
import com.jkoss.pojo.stu.Ossstu;
import com.jkoss.pojo.stu.OssstuExample;
import com.jkoss.pojo.stu.Results;
import com.jkoss.pojo.zx.Consultation;
import com.jkoss.pojo.zx.ConsultationExample;
import com.jkoss.tool.DbCommonMethod;
import com.jkoss.tool.Page;

@Component  // 
public class StusBiz implements IStusBiz{
	@Autowired
	private ClassesMapper claDao;
	@Autowired
	private OssstuMapper stuDao;
	@Autowired
	private ConsultationMapper conDao;
	@Autowired
	private EvaluateMapper evaDao;
	@Autowired
	private KaoshiMapper ksDao;
	@Autowired
	private ResultsMapper rsDao;

 
 
 

	//添加考核主题
	@Transactional
	public String addkaoshi(String filePath, int clzid, int empid) {
		// TODO Auto-generated method stub
		
		List<Kaoshi> kslist=readEccelKaoshi(filePath,clzid,empid);
	//	System.out.println("多少条数据"+kslist.size()+"  "+kslist.get(0).getThename()+kslist.get(1).getThename());
		for (Kaoshi kaoshi : kslist) {
			ksDao.insertks(kaoshi);//自己写的方法添加考试主题
		}
		return "添加成功";
	}
	
	public List<Kaoshi> readEccelKaoshi(String filePath,int clzid, int empid){
		
		List<Kaoshi> kslist=new ArrayList<>();
		//用poi解析xml
		POIFSFileSystem fs;
		try {
			fs = new POIFSFileSystem(new FileInputStream(filePath));
			//取到excel文档
			HSSFWorkbook kqXls = new HSSFWorkbook(fs);
			
			// 取得"考核主题"sheet
			HSSFSheet sheet0 = kqXls.getSheet("考核主题");
			
			//得到excel的信息,从第三行开始
			HSSFRow readrow= sheet0.getRow(2);
			int lastNum= sheet0.getLastRowNum();//得到最大行,设定的最大列是4
			
			HSSFCell cell= readrow.getCell(0);
			
			for(int i=2;i<=lastNum;i++){//从第三行开始读数据
				Kaoshi ks=new Kaoshi();
				readrow=sheet0.getRow(i);
				String ksname=readrow.getCell(0).getStringCellValue();
				Date ksdate=DbCommonMethod.isoFormat.parse(readrow.getCell(1).getStringCellValue());
				String sjurl=readrow.getCell(2).getStringCellValue();
				String dnurl=readrow.getCell(3).getStringCellValue();
					
				ks.setThename(ksname);
				ks.setThemetime(ksdate);
				ks.setSjurl(sjurl);
				ks.setSjansurl(dnurl);
				ks.setClzid(clzid);
				ks.setEid(empid);
				
				kslist.add(ks);
				System.out.println(ksname+" "+ksdate+" "+sjurl+" "+dnurl);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return kslist;
		
	}

 
	public List<Kaoshi> selectKaoshiByClzid(int clzid) {
		// TODO Auto-generated method stub
		/*KaoshiExample ks=new KaoshiExample();
		KaoshiExample.Criteria criteria= ks.createCriteria();
		criteria.andClzidEqualTo(clzid);*/
	//	System.out.println(ksDao.selectByExample(ks).size()+" sdfsdfsdfs");

		return ksDao.selectKsByClzid(clzid);
	}

 
	public Classes selectClznameByClzid(int clzid) {
		// TODO Auto-generated method stub
		return claDao.selectClanameByclzid(clzid);
	}

 
	public String upadateKspjAndBz(Kaoshi ks) {

		return ksDao.upadateKspjBz(ks)==1?"添加评价跟备注成功":"添加评价跟备注失败";
	}

 
	public List<Kaoshi> selectKhStuname(int ossstuid) {
		// TODO Auto-generated method stub
		return ksDao.selectKaoshiAndStuname(ossstuid);
	}

 
	public String addStuCj(Results rs) {
		// TODO Auto-generated method stub
		return rsDao.insert(rs)==1?"添加学生成绩成功":"添加学生成绩失败";
	}

 
	public String updateByOssstu(Ossstu stu) {
		// TODO Auto-generated method stub
		return stuDao.updateByossstu(stu)==1?"修改学生信息成功":"修改学生信息失败";
	}

 
	public List<Classes> listAllClass() {
		// TODO Auto-generated method stub
		
		return claDao.selectByExample(null);
	}

 
	public List<Ossstu> listAllStudents(Page<Ossstu> page) {
		// TODO Auto-generated method stub
		return stuDao.selectByPageClaid(page);
	}

 
	public void updateByStuidClzid(String stuid, Integer clzid) {
		// TODO Auto-generated method stub
		stuDao.updateByStuidClzid(stuid,clzid);
	}

 
	public Consultation selectByConsoluction(String stuname, String stuphone) {
		// TODO Auto-generated method stub
		ConsultationExample example=new ConsultationExample();
		 com.jkoss.pojo.zx.ConsultationExample.Criteria criteria= example.createCriteria();
		
		criteria.andStunameEqualTo(stuname);
		criteria.andStuphoneEqualTo(stuphone);

		List<Consultation> conlist= conDao.selectByExample(example);
		if(conlist.size()>0){
			return  conDao.selectByExample(example).get(0);
		}
		return  null;
	}

 
	public List<Classes> listAllClases(Page<Classes> page) {
		// TODO Auto-generated method stub
	//	System.out.println("查询了"+classesDao.findClassByEid(eid).size());
		return claDao.findClassByEid(page);
	}

 
	public String addClass(Classes cla) {
		// TODO Auto-generated method stub
		return claDao.insert(cla)>=1?"添加班级成功":"添加班级失败";
	}

 
	public String delCla(Integer clzid) {
		// TODO Auto-generated method stub
		return claDao.deleteByPrimaryKey(clzid)==1?"删除班级成功":"删除班级失败";
	}

 
	public List<Ossstu> selectStuByclzid(Integer clzid) {
		// TODO Auto-generated method stub
		
		OssstuExample osex=new OssstuExample();
		com.jkoss.pojo.stu.OssstuExample.Criteria criteria= osex.createCriteria();
		criteria.andClzidEqualTo(clzid);
		return stuDao.selectByExample(osex);
	}

 
	public int insertOssstu(Ossstu ossstu,int zxstuid) {
		// TODO Auto-generated method stub
		return stuDao.insertOssstu(ossstu,zxstuid);
	}

 
	public Ossstu selectStuByNamePhone(String stuname, String stucellphone) {
		// TODO Auto-generated method stub
		return stuDao.selectByNamePhone(stuname,stucellphone);
	}

 
	public Ossstu seletStuByOsid(int ossstuid) {
		// TODO Auto-generated method stub
		return stuDao.seletStuByOsid(ossstuid);
	}

 
	public String addEva(Evaluate eva) {
		// TODO Auto-generated method stub
		return evaDao.insert(eva)>0?"添加学员评价成功":"添加学员评价失败";
	}

 
	public List<Evaluate> selectEvalByOssstuId(int ossstuid) {
		// TODO Auto-generated method stub
		EvaluateExample eval=new EvaluateExample();
		EvaluateExample.Criteria criteria = eval.createCriteria();
		criteria.andOssstuidEqualTo(ossstuid);
		
		return evaDao.selectByExample(eval);
	}

}
